
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	"log"
	"time"
)

type FederationsFederationResolvers struct {
	FederationId       int64                               `db:"federation_id" json:"federationId"`
	FederationResolver int64                               `db:"federation_resolver" json:"federationResolver"`
	CreatedAt          time.Time                           `db:"created_at" json:"createdAt"`
	Links              FederationsFederationResolversLinks `json:"_links" db:-`
}

type FederationsFederationResolversLinks struct {
	Self string `db:"self" json:"_self"`
}

// @Title getFederationsFederationResolversById
// @Description retrieves the federations_federation_resolvers information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    FederationsFederationResolvers
// @Resource /api/2.0
// @Router /api/2.0/federations_federation_resolvers/{id} [get]
func getFederationsFederationResolver(federationId int64, federationResolver int64, db *sqlx.DB) (interface{}, error) {
	ret := []FederationsFederationResolvers{}
	arg := FederationsFederationResolvers{}
	arg.FederationId = federationId
	arg.FederationResolver = federationResolver
	queryStr := "select *, concat('" + API_PATH + "federations_federation_resolvers', '/federation_id/', federation_id, '/federation_resolver/', federation_resolver) as self"
	queryStr += " from federations_federation_resolvers WHERE federation_id=:federation_id AND federation_resolver=:federation_resolver"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getFederationsFederationResolverss
// @Description retrieves the federations_federation_resolvers
// @Accept  application/json
// @Success 200 {array}    FederationsFederationResolvers
// @Resource /api/2.0
// @Router /api/2.0/federations_federation_resolvers [get]
func getFederationsFederationResolvers(db *sqlx.DB) (interface{}, error) {
	ret := []FederationsFederationResolvers{}
	queryStr := "select *, concat('" + API_PATH + "federations_federation_resolvers', '/federation_id/', federation_id, '/federation_resolver/', federation_resolver) as self"
	queryStr += " from federations_federation_resolvers"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postFederationsFederationResolvers
// @Description enter a new federations_federation_resolvers
// @Accept  application/json
// @Param                 Body body     FederationsFederationResolvers   true "FederationsFederationResolvers object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/federations_federation_resolvers [post]
func postFederationsFederationResolver(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v FederationsFederationResolvers
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO federations_federation_resolvers("
	sqlString += "federation_id"
	sqlString += ",federation_resolver"
	sqlString += ",created_at"
	sqlString += ") VALUES ("
	sqlString += ":federation_id"
	sqlString += ",:federation_resolver"
	sqlString += ",:created_at"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putFederationsFederationResolvers
// @Description modify an existing federations_federation_resolversentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     FederationsFederationResolvers   true "FederationsFederationResolvers object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/federations_federation_resolvers/{id}  [put]
func putFederationsFederationResolver(federationId int64, federationResolver int64, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg FederationsFederationResolvers
	err := json.Unmarshal(payload, &arg)
	arg.FederationId = federationId
	arg.FederationResolver = federationResolver
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE federations_federation_resolvers SET "
	sqlString += "federation_id = :federation_id"
	sqlString += ",federation_resolver = :federation_resolver"
	sqlString += ",created_at = :created_at"
	sqlString += " WHERE federation_id=:federation_id AND federation_resolver=:federation_resolver"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delFederationsFederationResolversById
// @Description deletes federations_federation_resolvers information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    FederationsFederationResolvers
// @Resource /api/2.0
// @Router /api/2.0/federations_federation_resolvers/{id} [delete]
func delFederationsFederationResolver(federationId int64, federationResolver int64, db *sqlx.DB) (interface{}, error) {
	arg := FederationsFederationResolvers{}
	arg.FederationId = federationId
	arg.FederationResolver = federationResolver
	result, err := db.NamedExec("DELETE FROM federations_federation_resolvers WHERE federation_id=:federation_id AND federation_resolver=:federation_resolver", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
